package klk.shop.objects;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import klk.shop.utilities.MysqlUtilities;

public class BankAccount extends KLKObject {

	protected int id;
	protected String accountNumber;
	protected Bank bank;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getAccountNumber() {
		return accountNumber;
	}
	public void setAccountNumber(String accountNumber) {
		this.accountNumber = accountNumber;
	}
	
	public Bank getBank() {
		return bank;
	}
	public boolean setBank(Bank bank) {
		if (bank==null) {
			this.bank = new Bank();
			return false;
		} else {
			this.bank = bank;
			return true;
		}
		
	}
	
	
	public BankAccount(int id, String accountNumber, Bank bank) {
		super();
		this.accountNumber = accountNumber;
		this.bank = bank;
	}
	public BankAccount() {
		this(-1, "", new Bank());
	}
	
	@Override
	public String toString() {
		String shortenedAccountNumber=accountNumber.substring(0, accountNumber.length()-3)+"***";
		StringBuilder sb = new StringBuilder();
		sb.append( shortenedAccountNumber );
		sb.append( ' ' );
		sb.append( bank.toString() );
		return sb.toString();
	}
	@Override
	public String toHtml() {
		return toString();
	}
	
	
	public static ArrayList<BankAccount> getCustomersBankAccountListFromDatabase(int customerId) {
		ArrayList<BankAccount> bankAccountList = new ArrayList<BankAccount>();
			
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			rs = stmt.executeQuery(
				"SELECT " +
					"id, " + 
					"account_number, " +
					"bank_id " +
				"FROM " +
					"bank_accounts " +
				"WHERE customer_id='"+customerId+"';"
			);
			
			
			while (rs.next()) {
				bankAccountList.add(
					new BankAccount(
						rs.getInt("id"),	
						rs.getString("account_number"),
						Bank.getBankFromDatabase(
							Integer.parseInt(rs.getString("bank_id"))
						)
					)
				);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) try { rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
		return bankAccountList;	
	}
	
	public static int SetBankAccountIntoDatabase(BankAccount ba, Customer cu) {
		
		int rowsAffected = 0;

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
	
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			String insertString = 
					"INSERT INTO `bank_accounts` (" + 
						"customer_id, account_number, bank_id " +
					") VALUES (" +
							  cu.getCustomerId() 		+ ", " +  
						"'" + ba.getAccountNumber() 	+ "', " + 	  
							+ ba.bank.getId() 			+  
					");";
			System.out.println(insertString);
			rowsAffected = conn.createStatement().executeUpdate( insertString ); 
			
			stmt = conn.createStatement();
			rs = stmt.executeQuery(
				"SELECT LAST_INSERT_ID();"
			);
			rs.next();
			ba.setId(rs.getInt("LAST_INSERT_ID()"));
			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			if (  rs != null) try {   rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
			

		
		return rowsAffected;	
	}
	
	public static int DeleteBankAccountFromDatabase(BankAccount ba, Customer cu) {
		
		int rowsAffected = 0;

		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
	
		try	{
			
			conn= MysqlUtilities.getConnection();
			stmt = conn.createStatement();
			
			
			
			String insertString = 
					"DELETE FROM `bank_accounts` " +
					"WHERE " +
						"customer_id=" + cu.getCustomerId() + " AND " +
						"account_id=" + ba.getAccountNumber() + " AND " +  
						"bank_id=" + ba.getBank().getId() + 
					";";
			System.out.println(insertString);
			rowsAffected = conn.createStatement().executeUpdate( insertString ); 
			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			if (  rs != null) try {   rs.close(); } catch(Exception e) {}
			if (stmt != null) try { stmt.close(); } catch(Exception e) {}
			if (conn != null) try { conn.close(); } catch(Exception e) {}
	    }
			

		
		return rowsAffected;	
	}
	
	
	public static BankAccount tryParse(String accountNumber, String bankNumber) {
		BankAccount b = new BankAccount();
		try { b.accountNumber=accountNumber; } 									catch (Exception e) {}
		//try { b.bank=Bank.tryParse(bankNumber); } catch (Exception e) {}
		return b;
	}

	
}
